﻿using System.Windows;
using System.Windows.Controls;
using GameBoard;
using Microsoft.Win32;
using System.IO;
using System;
using System.Windows.Media.Imaging;
using System.Windows.Input;

namespace GameBoard
{
    /// <summary>
    /// Interaction logic for VisualPlacement.xaml
    /// </summary>
    public partial class TerrainPlacement : UserControl
    {
        TerrainViewModel _vm = VisualsViewModel.Instance.TerrainViewModel;
        public TerrainPlacement()
        {
            InitializeComponent();

            DataContext = _vm;
            PlaceVisuals_ListBox.ItemsSource = _vm.PlacedVisualsList;
            
        }

        private void SelectionMode_Button_Checked(object sender, RoutedEventArgs e)
        {
            _vm.ShapePlacementType = TerrainViewModel.ShapeMode.None;
        }

        private void DrawLine_Button_Checked(object sender, RoutedEventArgs e)
        {
            _vm.ShapePlacementType = TerrainViewModel.ShapeMode.Line;
        }

        private void DrawCone_Button_Checked(object sender, RoutedEventArgs e)
        {
            _vm.ShapePlacementType = TerrainViewModel.ShapeMode.Cone;
        }

        private void DrawSphere_Button_Checked(object sender, RoutedEventArgs e)
        {
            _vm.ShapePlacementType = TerrainViewModel.ShapeMode.Sphere;
        }

        private void TapeMeasure_Button_Checked(object sender, RoutedEventArgs e)
        {
            VisualsViewModel.Instance.TapeMeasurerActive = true;
        }

        private void TapeMeasure_Button_Unchecked(object sender, RoutedEventArgs e)
        {
            VisualsViewModel.Instance.TapeMeasurerActive = false;
        }

        private void DrawCube_Button_Checked(object sender, RoutedEventArgs e)
        {
            _vm.ShapePlacementType = TerrainViewModel.ShapeMode.Square;
        }

        private void SetImageForVisual_Button_Click(object sender, RoutedEventArgs e)
        {
            var o = new OpenFileDialog();
            o.InitialDirectory = Directory.GetCurrentDirectory() + @"\3DObjects\ObjectVisuals\";
            o.Multiselect = false;
            o.ShowDialog();
            if (!String.IsNullOrWhiteSpace(o.FileName))
            {
                _vm.ObjectPlacementImageFile = o.FileName;
            }
            else
            {

                _vm.ObjectPlacementImageFile = "";
            }
        }

        private static String _fileSaveFilter = "cobj files (*.cobj)|*.cobj";
        private void LoadObjFile_Click(object sender, RoutedEventArgs e)
        {
            var o = new OpenFileDialog();
            o.Filter = _fileSaveFilter;
            o.InitialDirectory = Directory.GetCurrentDirectory() + @"\3DObjects\";
            o.Multiselect = false;
            o.ShowDialog();
            if (!String.IsNullOrWhiteSpace(o.FileName))
            {
                _vm.CurrentObjectFile = o.FileName;
                _vm.ShapePlacementType = TerrainViewModel.ShapeMode.ObjectFile;
            }
        }

        private void ZoomToPlacedVisual_Click(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                foreach (var cur in PlaceVisuals_ListBox.SelectedItems)
                {
                    var selectedData = cur as BaseTerrainData;
                    VisualsViewModel.Instance.ZoomToPlacedTerrainVisual
                        (selectedData.FactoryID, BoardsViewModel.BoardTypes.All);
                    VisualsViewModel.Instance.HighlightPlacedTerrainVisual
                        (selectedData.FactoryID, BoardsViewModel.BoardTypes.All);
                    
                }
            }
        }
	
        private void DeletePlacedVisual_Click(object sender, RoutedEventArgs e)
        {
            var selectedData = (sender as Button).DataContext as BaseTerrainData;
            VisualsViewModel.Instance.RemovePlacedTerrainVisual
            (selectedData);
        }
		
    }
}